// Copyright (C) 1998-2008, Sumisho Computer Systems Corp. All Rights Reserved.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @author Hitoshi Okada
 */

package com.curlap.orb.context;

import java.lang.reflect.InvocationTargetException;
import javax.servlet.ServletContext;

import org.apache.commons.beanutils.MethodUtils;

/**
 * Spring2ApplicationContext
 *   spring2 framework integration
 *   http://www.springframework.org/
 */
public class Spring2ApplicationContext extends AbstractApplicationContext 
{
    // org.springframework.web.context.WebApplicationContext
    private Object context = null;

    public Spring2ApplicationContext(ServletContext context) throws ApplicationContextException
    {
        try 
        {
            // org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(context);
            Class cls = Class.forName("org.springframework.web.context.support.WebApplicationContextUtils");
            this.context = MethodUtils.invokeStaticMethod(cls, "getRequiredWebApplicationContext", context);
        } 
        catch (ClassNotFoundException ex) 
        {
            throw new ApplicationContextException(ex);
        } 
        catch (NoSuchMethodException ex) 
        {
            throw new ApplicationContextException(ex);
        } 
        catch (IllegalAccessException ex) 
        {
            throw new ApplicationContextException(ex);
        } 
        catch (InvocationTargetException ex) 
        {
            throw new ApplicationContextException(ex);
        }
    }

    @Override
    public Object getObject(String name) throws ApplicationContextException 
    {
        try 
        {
            // return context.getBean(name);
            return MethodUtils.invokeMethod(context, "getBean", new Object[]{ name });
        }
        catch (NoSuchMethodException ex) 
        {
            throw new ApplicationContextException(ex);
        } 
        catch (IllegalAccessException ex)
        {
            throw new ApplicationContextException(ex);
        } 
        catch (InvocationTargetException ex) 
        {
            throw new ApplicationContextException(ex);
        }
    }

    @Override
    public void setObject(String name, Object value) throws ApplicationContextException
    {
        // do nothing (should throw exception?)
    }

    @Override
    public String[] getObjectNames() throws ApplicationContextException
    {
        try 
        {
            // return ((ApplicationContext)this.context).getBeanDefinitionNames();
            return (String[])MethodUtils.invokeMethod(context, "getBeanDefinitionNames", null);
        } 
        catch (NoSuchMethodException ex) 
        {
            throw new ApplicationContextException(ex);
        } 
        catch (IllegalAccessException ex) 
        {
            throw new ApplicationContextException(ex);
        } 
        catch (InvocationTargetException ex)
        {
            throw new ApplicationContextException(ex);
        }
    }

    @Override
    public Class getObjectType(String name) throws ApplicationContextException
    {
        try 
        {
            // return ((ApplicationContext)this.context).getType();
            return (Class)MethodUtils.invokeMethod(context, "getType", new String[]{ name });
        }
        catch (NoSuchMethodException ex)
        {
            throw new ApplicationContextException(ex);
        } 
        catch (IllegalAccessException ex)
        {
            throw new ApplicationContextException(ex);
        } 
        catch (InvocationTargetException ex) 
        {
            throw new ApplicationContextException(ex);
        }
    }
}
